package com.jy.api.service.impl;

import com.jy.api.dao.AppraiseNetDao;
import com.jy.api.domain.Appraise;
import com.jy.api.domain.AppraiseNet;
import com.jy.api.service.AppraiseNetService;
import com.jy.api.signUpReq.MarsCallReq.AppraiseNetRes;
import com.jy.api.util.MyDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * Created by xiaohua on 2018/11/10.
 */
@Service
public class AppraiseNetServiceImpl implements AppraiseNetService {

    @Autowired
    private AppraiseNetDao netDao;

    @Override
    public Page<AppraiseNet> findByStoreId(String storeId,String type,String year,String month, Pageable pageable) {

        if (StringUtils.equals(type, "MONTH")) {
            String s = year + "-" + month;
            Date minMonthDate = MyDateUtils.getMinMonthDate(s);
            Date maxMonthDate = MyDateUtils.getMaxMonthDate(s);
            Page<AppraiseNet> net = netDao.findByStoreIdAndAppraiseTimeBetweenOrderByAppraiseTimeDesc(storeId,minMonthDate,maxMonthDate, pageable);
            return net;
        } else {
            Page<AppraiseNet> net = netDao.findByStoreIdOrderByAppraiseTimeDesc(storeId, pageable);
            return net;
        }

    }

    /**
     * 网吧评价
     *
     * @param netRes
     */
    @Override
    @Transactional
    public void appraiseStore(AppraiseNetRes netRes) {
        if (!StringUtils.isBlank(netRes.getMessage())){
            AppraiseNet getAppraise = new AppraiseNet(netRes);
            netDao.save(getAppraise);
        }

    }

}
